package com.zj.leetcode.easy.tree;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

/**
 * @program: algorithm
 * @description: 两数之和 IV - 输入 BST
 * 653 two-sum-iv-input-is-a-bst
 * @author: Zhang Bo
 * @create: 2022-06-20 10:56:33
 **/
public class TwoSumIvInputIsABst {
    public static void main(String[] args) {
        Solution solution = new TwoSumIvInputIsABst().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {


        public boolean findTarget(TreeNode root, int k) {
            if (root == null) {
                return false;
            }
            Set<Integer> set = new HashSet<>();
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            while (!queue.isEmpty()) {
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode poll = queue.poll();
                    if(poll != null){
                        int sub = k - poll.val;
                        if(set.contains(poll.val)){
                            return true;
                        } else {
                            set.add(sub);
                        }
                        if(poll.left != null){
                            queue.add(poll.left);
                        }
                        if(poll.right != null){
                            queue.add(poll.right);
                        }
                    }
                }
            }
            return false;
        }


    }
//leetcode submit region end(Prohibit modification and deletion)

}